#include <iostream>
#include <vector>

struct Vec {
    std::vector<double> elems;

    double operator[](int i) {
        return elems[i];
    }
};

struct VecSum {
    Vec u;
    VecSum* v;  //链表

    double operator[](int i) {
        if(v == nullptr)
            return u[i];

        return u[i] + (*v)[i];
    }
};

VecSum* operator+(const Vec& u, const Vec& v) {
    VecSum* vs1 = new VecSum{u, nullptr};
    VecSum* vs2 = new VecSum{v, vs1};

    return vs2;
}

VecSum* operator+(VecSum* u, const Vec& v) {
    VecSum* vs1 = new VecSum{v, nullptr};
    if(u->v != nullptr)
        u->v->v = vs1;
    else
        u->v = vs1;

    return u;
}

int main() {
    Vec v0 = { {1.0, 1.0, 1.0} };
    Vec v1 = { {2.0, 2.0, 2.0} };
    Vec v2 = { {3.0, 3.0, 3.0} };

    VecSum* w = v0 + v1 + v2;
    double a = (*w)[1];

    std::cout << a << std::endl;

    return 0;
}